class Member:
    """会员类，包含会员等级和折扣率"""

    def __init__(self, name, level):
        self.name = name
        self.level = level  # 1: 普通会员, 2: 银卡会员, 3: 金卡会员, 4: 钻石会员

    @property
    def discount_rate(self):
        """根据会员等级返回折扣率"""
        if self.level == 1:
            return 0.95  # 普通会员 95折
        elif self.level == 2:
            return 0.90  # 银卡会员 9折
        elif self.level == 3:
            return 0.85  # 金卡会员 85折
        elif self.level == 4:
            return 0.80  # 钻石会员 8折
        else:
            return 1.0  # 非会员无折扣


class Product:
    """商品类"""

    def __init__(self, id, name, price):
        self.id = id
        self.name = name
        self.price = price

    def __str__(self):
        return f"{self.id}: {self.name} - ￥{self.price:.2f}"


class CartItem:
    """购物车项"""

    def __init__(self, product, quantity, member=None):
        self.product = product
        self.quantity = quantity
        self.member = member

    @property
    def subtotal(self):
        """计算单项小计"""
        if self.member:
            return self.product.price * self.quantity * self.member.discount_rate
        return self.product.price * self.quantity

    def __str__(self):
        discount_info = ""
        if self.member:
            discount_info = f" (会员折扣: {self.member.name}, 等级{self.member.level}, {self.member.discount_rate * 10:.1f}折)"
        return f"{self.product} × {self.quantity} = ￥{self.subtotal:.2f}{discount_info}"


class ShoppingCart:
    """购物车类"""

    def __init__(self):
        self.items = []

    def add_item(self, product, quantity, member=None):
        """添加商品到购物车"""
        self.items.append(CartItem(product, quantity, member))

    def remove_item(self, product_id):
        """从购物车移除商品"""
        self.items = [item for item in self.items if item.product.id != product_id]

    @property
    def total(self):
        """计算总价(不含折扣)"""
        return sum(item.product.price * item.quantity for item in self.items)

    @property
    def discounted_total(self):
        """计算折扣后总价"""
        return sum(item.subtotal for item in self.items)

    @property
    def total_discount(self):
        """计算总折扣金额"""
        return self.total - self.discounted_total

    def show(self):
        """显示购物车内容"""
        print("=== 购物车内容 ===")
        if not self.items:
            print("购物车为空")
            return

        for item in self.items:
            print(item)

        print("-----------------")
        print(f"商品总价: ￥{self.total:.2f}")
        print(f"会员折扣: -￥{self.total_discount:.2f}")
        print(f"应付总额: ￥{self.discounted_total:.2f}")
        print("=================")


# 示例使用
if __name__ == "__main__":
    # 创建一些商品
    products = [
        Product(1, "苹果", 5.5),
        Product(2, "牛奶", 3.8),
        Product(3, "面包", 7.2),
        Product(4, "鸡蛋", 0.5),
        Product(5, "牛肉", 32.0)
    ]

    # 创建会员
    regular_member = Member("张三", 1)  # 普通会员
    silver_member = Member("李四", 2)  # 银卡会员
    gold_member = Member("王五", 3)  # 金卡会员
    diamond_member = Member("赵六", 4)  # 钻石会员

    # 创建购物车
    cart = ShoppingCart()

    # 添加商品到购物车，部分商品使用会员折扣
    cart.add_item(products[0], 2)  # 苹果，不使用会员折扣
    cart.add_item(products[1], 3, regular_member)  # 牛奶，普通会员
    cart.add_item(products[2], 1, silver_member)  # 面包，银卡会员
    cart.add_item(products[3], 10, gold_member)  # 鸡蛋，金卡会员
    cart.add_item(products[4], 2, diamond_member)  # 牛肉，钻石会员

    # 显示购物车内容
    cart.show()